package weaver.interfaces.shuigui.workflow.action;

import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


import java.text.SimpleDateFormat; 
import java.util.Date;             

/**
 * @author : hmy
 * @since : 2025/6/20
 */
public class MeetingBatchInsertFormMode implements Action {

    private String closeDelete;

    // 出席字段名
    private String cx = "cx";
    // 请假字段名
    private String qj = "qj";
    // 会议类型字段名
    private String hylx = "hylx";
    @Override
    public String execute(RequestInfo requestInfo) {
        BaseBean bb = new BaseBean();
        String requestId = requestInfo.getRequestid();
        RequestManager requestManager = requestInfo.getRequestManager();
        String src = requestManager.getSrc();
        bb.writeLog("==========MeetingBatchInsertFormMode start === src:" + src + "  requestId:" + requestId + " closeDelete:"+closeDelete);
        RecordSet rs = new RecordSet();
        if (!"submit".equalsIgnoreCase(src)) {
            if (!"1".equalsIgnoreCase(closeDelete)) {
                // 未关闭， 执行删除操作
                rs.executeUpdate("delete from uf_hyryshqk where hylc = ?", requestId);
            }
            return Action.SUCCESS;
        }

        // 出席字段值
        String attendFieldValue = "";
        // 请假字段值
        String absentFieldValue = "";
        // 会议类型字段值
        String meetingTypeFieldValue = "";
        MainTableInfo mainTableInfo = requestInfo.getMainTableInfo();
        for (Property property : mainTableInfo.getProperty()) {
            if (property.getName().equalsIgnoreCase(cx)) {
                attendFieldValue = property.getValue();
            }
            if (property.getName().equalsIgnoreCase(qj)) {
                absentFieldValue = property.getValue();
            }
            if (property.getName().equalsIgnoreCase(hylx)) {
                meetingTypeFieldValue = property.getValue();
            }
        }
        // 先删除
        rs.executeUpdate("delete from uf_hyryshqk where hylc = ?", requestId);



        // 获取建模表单数据id
        int modeId = Util.getIntValue(getModeIdByTableName(rs, "uf_hyryshqk"));

        String date = new SimpleDateFormat("yyyy年MM月dd日").format(new Date());
        String time = TimeUtil.getOnlyCurrentTimeString();


        List<List> insertValue = new ArrayList<>();
        String[] absentValueArr = absentFieldValue.split(",");
        for (int i = 0; i < absentValueArr.length; i++) {
            List value =  new ArrayList();
            value.add(absentValueArr[i]);
            value.add(requestId);
            value.add("1"); // 请假状态
            value.add(meetingTypeFieldValue);
            value.add("1");


            value.add(modeId);
            value.add(UUID.randomUUID().toString().replaceAll("-", ""));
            value.add("1");
            value.add(date);
            value.add(time);
            value.add("0");
            insertValue.add(value);
        }

        String[] attendValueArr = attendFieldValue.split(",");
        for (int i = 0; i < attendValueArr.length; i++) {
            List value =  new ArrayList();
            value.add(attendValueArr[i]);
            value.add(requestId);
            value.add("0"); // 出席状态
            value.add(meetingTypeFieldValue);
            value.add("1");

            value.add(modeId);
            value.add(UUID.randomUUID().toString().replaceAll("-", ""));
            value.add("1");
            value.add(date);
            value.add(time);
            value.add("0");
            insertValue.add(value);
        }
        bb.writeLog("MeetingBatchInsertFormMode insert data: " + JSONObject.toJSONString(insertValue));
        boolean flag = rs.executeBatchSql("insert into uf_hyryshqk(hyry, hylc, zt, hylx, sfyx,FORMMODEID," +
                        "MODEUUID,MODEDATACREATER,MODEDATACREATEDATE,MODEDATACREATETIME,MODEDATACREATERTYPE) values " +
                        "(?,?,?,?,?,?,?,?,?,?,?)",  insertValue);
        if (!flag) {
            bb.writeLog("MeetingBatchInsertFormMode insert error: " + rs.getMsg());
        } else {


            // 获取建模表单数据id
            RecordSet rsMode = new RecordSet();
            // 获取新插入的数据id
            String queryNewBillIdSql = "select id from uf_hyryshqk where hylc = ?";

            rsMode.executeQuery(queryNewBillIdSql, requestId);

            List<String> newBillIdList = new ArrayList<>();
            while (rsMode.next()) {
                newBillIdList.add(rsMode.getString(1));
            }

            bb.writeLog("requestid: " + requestId + " ======  newBillIdList: " + JSONObject.toJSONString(newBillIdList));
            ModeRightInfo modeRightInfo = new ModeRightInfo();
            for (String billId : newBillIdList) {
                modeRightInfo.setNewRight(true);
                modeRightInfo.editModeDataShare(1, modeId, Util.getIntValue(billId));
                //新建的时候添加共享
                modeRightInfo.addDocShare(1, modeId, Util.getIntValue(billId));//新建的时候添加文档共享
            }
        }
        return Action.SUCCESS;
    }

    /**
     * 通过建模表表名获取所属模块id
     *
     * @param rs
     * @param tablename
     * @return
     */
    public static String getModeIdByTableName(RecordSet rs, String tablename) {
        String modeid = "0";
        rs.executeQuery("select id from modeinfo where formid=(select id from workflow_bill where tablename=?)", tablename);
        if (rs.next()) {
            modeid = Util.null2String(rs.getString("id"));
        }
        return modeid;
    }
}
